let leftlegend = [], rightlegend = [], total = 0, count = 0;
let duration = 2000, timer = null;
// 图例分区
data.forEach(item => {
    total += Number(item.value);
})
if(total > 0){
    data.forEach(item => {
        const per = Number(item.value) / total;
        if(count < 0.5){
            rightlegend.push(item.name);
            count += per
        } else {
            leftlegend.unshift(item.name);
            count += per
        }
    })
}


var options = this.chart.getOption();

options.series[0].label =  {
    normal: {
        show: false
    },
    emphasis:  {
        show: false
    },
};
const color = ['#74f2e6', '#cef197', '#68cdfa', '#617ce8',
    '#f9cc64', '#ec4c94', '#894df6', '#db715e'];
options.color = color;

options.legend = [{
    orient: 'vertical',
    top: 'center',
    left: 0,
    itemGap: (210 - 16 * leftlegend.length) / (leftlegend.length - 1),
    icon: 'circle',
    data: leftlegend,
    textStyle: {
        color: "#fff",
        fontWeight: 'normal',
        fontSize: 16
    },
    formatter: function(name) {
        const per = data.filter(item=> item.name === name)[0].value / total;
        return name + ' '+(per * 100).toFixed(1)+'%';
    }
},
    {
        orient: 'vertical',
        top: 'center',
        right: 10,
        itemGap: (210 - 16 * rightlegend.length) / (rightlegend.length - 1),
        icon: 'circle',
        data: rightlegend,
        textStyle: {
            color: "#fff",
            fontWeight: 'normal',
            fontSize: 16,
        },
        formatter: function(name) {
            const per = data.filter(item=> item.name === name)[0].value / total;
            return name + ' '+(per * 100).toFixed(1)+'%';
        }
    }
];

options.series = [
    {
        name: ' 舆情媒体预警',
        roseType: false,
        hoverAnimation: false,
        zIndex: 99,
        minAngle: 10,
        type: 'pie',
        center: ['45%', '50%'],
        radius: ['40%', '55%'],
        data: data,
        label: {
            normal: {
                show: false
            },
            emphasis:  {
                show: false
            },
        }
    },
    {
        name: '舆情媒体预警',
        roseType: false,
        type: 'pie',
        zIndex: 100,
        minAngle: 10,
        center: ['45%', '50%'],
        radius: ['40%', '55%'],
        data: data,
        itemStyle: {
            opacity: 0.5
        },
        labelLine: {
            normal: {
                show: false
            }
        },
        label: {
            normal: {
                show: false
            },
            emphasis:  {
                show: false
            },
        }
    },
    {
        name: 'line',
        type: 'pie',
        hoverAnimation: false,
        center: ['45%', '50%'],
        radius: [66, 67],
        color: ['#58C7FF'],
        data: _.take(data, 1),
        tooltip:{
            show: false
        },
        labelLine: {
            normal: {
                show: false
            }
        },
        label: {
            normal: {
                position: 'center',
                formatter: [
                    '{a|{c}%}',
                    '{b}'
                ].join('\n'),
                show: true,
                textStyle: {
                    fontWeight: 'bold',
                    fontSize: 16,
                    color: '#fff'
                },
                rich: {
                    a: {
                        fontSize: 16,
                        color: '#EAAD43',
                        padding: 10
                    }
                }
            }
        },
    }
];
this.chart.setOption(options);


let chart = this.chart;
let i = 0;
change();
function change() {
    // // 默认高亮
    chart.dispatchAction({
        type: 'highlight',
        // 可选，系列 index，可以是一个数组指定多个系列
        seriesIndex: 1,
        // 可选，系列名称，可以是一个数组指定多个系列
        // seriesName: string|Array,
        // 可选，数据的 index
        dataIndex: i,
        // 可选，数据的 名称
        // name: string
    })
    chart.dispatchAction({
        type: 'downplay',
        // 可选，系列 index，可以是一个数组指定多个系列
        seriesIndex: 1,
        dataIndex: i == 0 ? (data.length - 1) : (i - 1)
    })
}
function auto(){
    timer = setInterval(function(){
        i++;
        if(i>=data.length){
            i = 0;
        }
        // 替换中心文字
        options.series[2].data = [data[i % data.length]];
        chart.setOption(options);

        change();


    }, duration)
}
auto();


chart.on('mouseover', function(){
    clearInterval(timer);
})
chart.on('mouseLeave', function(){
    auto();
})


